
import unittest

import config_service

class ConfigServiceTest(unittest.TestCase):

  def test_config_service(self):
    # Test that there is an initial template in the store.
    dummy9000  = {
      'key': 'dummy9000',
      'description': 'This is just a dummy template for testing.',
      'to': {'dummy@xcorp.com'},
      'cc': {'the_coworker.xcorp.com'},
      'subject': '{{topic}}',
      'body': 'Hello\n      {{topic}}\n\n      To inform you of:\n      '
              '{{information}}',
    }
    self._test_get('dummy9000', dummy9000)

    # Test that a listing of templates has only the initial template entry.
    dummy_template_abstract = {
      'description': 'This is just a dummy template for testing.',
      'key': 'dummy9000'
    }
    self._test_list([dummy_template_abstract])

    # Test for the creation of a template.
    template_candidate = {
      'key': 'candidate-001',
      'description': 'This is just a candidate template for testing.',
      'to': {'candidate@xcorp.com'},
      'cc': {'the_coworker@xcorp.com'},
      'subject': 'This is about {{topic}}',
      'body': 'Greetings\n      {{topic}}\n\n      To inform you of:\n      '
              '{{some_data}}',
    }
    config_service.CreateEmailTemplate(**template_candidate)
    self._test_get('candidate-001', template_candidate)

    # Test that the newly created template is is visible via the listing.
    template_candidate_abstract = {
      'key': 'candidate-001',
      'description': 'This is just a candidate template for testing.',
    }
    self._test_list([dummy_template_abstract, template_candidate_abstract])

    # Test an update.
    template_candidate['to'].add('boss@xcorp.com')
    template_candidate['subject'] = 'Regarding: {{topic}}'
    config_service.UpdatedEmailTemplate(**template_candidate)

    self._test_get('candidate-001', template_candidate)

    # Test the deletion.
    config_service.DeleteEmailTemplate('candidate-001')

    # Test retrieval of deleted item.
    self.assertRaises(KeyError,
                      config_service.GetEmailTemplate,
                      'candidate-001')

    # Test the the deleted entry is no longer on the list.
    self._test_list([dummy_template_abstract,])

  def test_render(self):
    template = 'Hello {{name}}!'
    data = {'name': 'World'}

    output = config_service.ResolveTemplate(template, data)

    self.assertIsNotNone(output)
    expected_output = 'Hello World!'
    self.assertEqual(expected_output, output)

  def _test_get(self, key, expected_template):
    template = config_service.GetEmailTemplate(key)

    self.assertIsNotNone(template,
                         ('GetEmailTemplate(%s) failed.' % key ))
    self.assertDictEqual(expected_template, template)

  def _test_list(self, expected_list):
    # Get the current list of templates.
    templates = config_service.ListEmailTemplates()

    # Sort the list by key to ensure proper equality testing of template items.
    expected_list = sorted(expected_list, key=lambda k: k['key'])
    templates = sorted(templates, key=lambda k: k['key'])

    self.assertListEqual(expected_list, templates)


if __name__ == '__main__':
  unittest.main()
